package io.xiaojl.xj2ee.modules.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.xiaojl.xj2ee.modules.sys.entity.SysRoleMenuEntity;
import io.xiaojl.xj2ee.modules.sys.mapper.SysRoleMenuEntityMapper;
import io.xiaojl.xj2ee.modules.sys.service.SysRoleMenuService;


@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
	@Autowired
	private SysRoleMenuEntityMapper sysRoleMenuMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
		//先删除角色与菜单关系
		deleteBatch(new Long[]{roleId});

		if(menuIdList.size() == 0){
			return ;
		}

		//保存角色与菜单关系
		for(Long menuId : menuIdList){
			SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
			sysRoleMenuEntity.setMenuId(menuId);
			sysRoleMenuEntity.setRoleId(roleId);
			
			sysRoleMenuMapper.insertSelective(sysRoleMenuEntity);
		}
	}

	@Override
	public List<Long> queryMenuIdList(Long roleId) {
		return sysRoleMenuMapper.queryMenuIdList(roleId);
	}

	@Override
	public int deleteBatch(Long[] roleIds){
		return sysRoleMenuMapper.deleteBatch(roleIds);
	}

	@Override
	public void deleteByMenuId(Long menuId) {
		sysRoleMenuMapper.deleteByMenuId(menuId);
	}
}
